<template>
  <div class="login-container">
    <h2>登录</h2>
    <el-form class="login-form">
      <el-form-item label="用户名">
        <el-input v-model="username" placeholder="请输入用户名"></el-input>
      </el-form-item>
      <el-form-item label="密码">
        <el-input v-model="password" type="password" placeholder="请输入密码"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="handleLogin">登录</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import { userApi } from '@/utils/request'
export default {
  name: 'Login',
  data() {
    return {
      username: '',
      password: ''
    }
  },
  methods: {
    handleLogin() {
      userApi.login(this.username, this.password).then(res => {
        if (res && res.data && res.data.data) {
          this.$router.push('/');
        }
      }).catch(err => {
        console.error('登录失败:', err);
      });
    }
  }
}
</script>

<style scoped>
.login-container {
  max-width: 350px;
  margin: 60px auto;
  padding: 30px;
  border: 1px solid #eee;
  border-radius: 8px;
  background: #fff;
}
.login-form {
  max-width: 400px;
  margin: 60px auto;
  padding: 30px;
  border: 1px solid #eee;
  border-radius: 8px;
  background: #fff;
}
</style>